﻿using System;
using System.Collections.Generic;
using EPiServer.Cms.Shell.Extensions;
using EPiServer.Core;
using EPiServer.Framework.Localization;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;

namespace OpenWaves.EPiServer.BrightcoveVideoCloudProperty
{
    [EditorDescriptorRegistration(TargetType = typeof (global::EPiServer.Url), UIHint = VideoCloudProperty.UIHint)]
    public class PropertyVideoCloudEditorDescriptor : EditorDescriptor
    {
        public PropertyVideoCloudEditorDescriptor()
        {
            ClientEditingClass = "ow-epi-videocloud.Selector";
        }

        public override void ModifyMetadata(ExtendedMetadata metadata,
                                            IEnumerable<Attribute> attributes)
        {
            base.ModifyMetadata(metadata, attributes);
            ExtendedMetadata mostContentMetadata = metadata.FindTopMostContentMetadata();
            var content = mostContentMetadata.Model as IContent;
            if (content == null)
                return;
            metadata.EditorConfiguration.Add("contentTypeId", content.ContentTypeID);
            if (content.ContentLink != null)
                metadata.EditorConfiguration.Add("contentLink", content.ContentLink);

            metadata.EditorConfiguration.Add("browseVideos",
                                             LocalizationService.Current.GetString("/videoCloudProperty/browseVideos",
                                                                                   "Browse videos"));

            metadata.CustomEditorSettings["ImageDialogClassName"] = "ow-epi-videocloud.BrowserDialog";
            metadata.CustomEditorSettings["ImageDialogSettings"] = new
                {
                    FileControllerUrl = BrowserController.GetControllerPath(),
                    Players = VideoCloudService.Configuration.PlayersList
                };
        }
    }
}